/*
 * This file is part of Shopping Lists Organizer software
 *
 * Copyright (C) 2011 Denis Kvita <dkvita@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PURCHASE_H
#define PURCHASE_H

#include <QAbstractTableModel>
#include <QSqlRecord>
#include <QStringList>
#include <QList>
#include <QDateTime>

class QDate;
class QPrinter;
class QSqlQuery;

class purchase : public QAbstractTableModel
{
	Q_OBJECT

public:
	explicit purchase(QObject *parent = 0);

	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	int columnCount(const QModelIndex &parent = QModelIndex()) const;
	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
	bool setData(const QModelIndex &index, const QVariant &value, int role);
	Qt::ItemFlags flags(const QModelIndex & index) const;
	bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex());
	bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());

	void clear();
	void setList(const QString listName);
	void restore(const QDate date, const QString listName);
	QModelIndex create(QVariant shop, QVariant sec, QVariant ctg, QVariant title, QVariant planPrc, QVariant planCnt, QVariant buyPrc, QVariant buyCnt, QVariant joy, QVariant done);
	QModelIndex addEmpty();
	bool rem(int row);
	int rowid(int row);
	void print(QPrinter *printer, bool onlyUnchecked= false);
	double totalPlan();
	double totalBuy();
	bool reportErr(QSqlQuery &q);

signals:
	void totalPlanChanged(double newTotal);
	void totalBuyChanged(double newTotal);

public slots:

private:
	QString wList;
	QDate wDate;
	QStringList fieldNames;
	int lastShop;
	QString lastSec,lastCat;
	QList<QSqlRecord> cc;
};

#endif // PURCHASE_H
